#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "awinic_audio.h"

#define MAX_LINE 1024
#define AUDIO_CNT 1024
#define INPUT_FILE "./vin_24bit.pcm"
#define OUTPUT_FILE "./awinic_out.pcm"

long int read_data(char *buf,ssize_t size,long int pos)
{

	int i;
        long int ret;
        FILE*fp;
	


	fp =fopen(INPUT_FILE,"rb");
	if(fp == NULL)
	{
		printf("%s: fopen input file failed \n",__func__);
		return -1;
	}
	
    fseek(fp,pos,SEEK_SET);
  
    ret = fread(buf,sizeof(char),size,fp);
      
    ret = ftell(fp);
       //printf("%s:read number = %d \n",__func__,i);
	fclose(fp);
	return ret;
}

int write_data(char *buf,ssize_t size)
{
	ssize_t i;
        FILE*fp;
	
	fp = fopen(OUTPUT_FILE,"ab");
        if(fp == NULL)
	{
		printf("%s: fopen outout file failed \n",__func__);
		return -1;
	}
	
	fwrite(buf,sizeof(char),size,fp);

	fclose(fp);
        //printf("%s:write number = %ld \n",__func__,i);
	return 0;
}
int main(void)
{
    ssize_t i,j;
    int32_t *in_data=NULL;
    char* cfg_ptr;
    long int pos =0;
    in_data = (int32_t*)calloc(AUDIO_CNT*2+1,sizeof(int32_t));
    if(in_data== NULL){
       printf("%s: malloc in data failed \n",__func__);
       exit(1);
    }

 
    i = AwinicGetSize();
    if(i>0)
    {
        cfg_ptr = (char*)calloc(i,sizeof(char));
    }
    else{
        printf("get size of cfg failed\n");
    }

    AwinicAlgoInit(cfg_ptr);

    for(i=0;i<1920;i++){
    pos = read_data((char*)in_data,AUDIO_CNT*2,pos);
    if(pos <0)
    {
        printf("%s:read data failed \n",__func__);
        exit(1);
    }
    AwinicAudioHandle((void*)in_data,AUDIO_CNT*2,cfg_ptr);
    write_data((char*)in_data,AUDIO_CNT*2);
    }

    AwinicAlgoDeinit(cfg_ptr);

    free(cfg_ptr);
    free(in_data);
	
    exit(1);
}
